let BaseController = require('./BaseController')
let fs = require('fs')
let path = require('path')
class Controller extends BaseController {
    constructor() {
        super();
        this.baseSrc = `d:/数据指标分类/${new Date().format('yyyyMMdd')}/待入库/04技术指标/买入信号/`;
        this.encode = 'UTF-8'
        this.params = {
            pageIndex: 1,
            pageTotal: ''
        }
        this.data = []
    };
    /**
     * 获取url
     */
    getUrl(options) {
        return `http://www.iwencai.com/stockpick/cache?token=${this.params.token}&p=${this.params.pageIndex}0&perpage=30&showType=[%22%22,%22%22,%22onTable%22,%22onTable%22,%22onTable%22,%22onTable%22,%22onTable%22]`
    }

    async getParams() {
        let self = this;
        let resultArr = []
        await this.page.goto(`http://www.iwencai.com/stockpick/search?typed=1&preParams=&ts=1&f=3&qs=result_original&selfsectsn=&querytype=stock&searchfilter=&tid=stockpick&w=%E4%B9%B0%E5%85%A5%E4%BF%A1%E5%8F%B7&queryarea=`)
        await this.page.waitForSelector('#tableWrap')
        await this.sleep(2000)
        let data = await this.page.evaluate(function () {
            return allResult.result;
        })
        resultArr.push(...data)
        let i = 0;
        function getPage() {
            return new Promise((resolve, reject) => {
                async function get() {
                    try {
                        await self.page.addScriptTag({
                            content: `
                            $('.atomic_table_con').remove();
                            $('.num.current').next().trigger('click')
                            `
                        })
                        await self.page.waitForSelector('.atomic_table_con')
                        let data = await self.page.evaluate(function () {
                            return allResult.result;
                        })
                        resultArr.push(...data)
                        let length = await self.page.evaluate(function () {
                            return $('.num.current').next('.num').length;
                        })
                        if (length) {
                            i++
                            await self.sleep(3000)
                            await get()
                        } else {
                            resolve()
                        }
                    } catch (err) {
                        reject()
                    }
                }
                get()
            })
        }
        await getPage();
        this.codeList = resultArr
        console.log(this.codeList.length)
    };


    /**
     * 获取历史数据
     */
    async getHistory() {
        console.log('99买入信号Controller')
    };


    /**
     * 写csv
     */
    writeCsv(arr) {
        for (let i = 0; i < this.codeList.length; i++) {
            this.codeList[i][0] = this.codeList[i][0].match(/^\d+/)[0]
        }

        if (!fs.existsSync(this.baseSrc)) {
            this.mkdirs(this.baseSrc)
        }
        if (this.codeList && this.codeList.length) {
            let content = `code,name,现价,涨跌幅,买入信号,技术形态,选股动向\n`;
            content += this.codeList.join('\n');
            fs.writeFileSync(path.join(this.baseSrc, `买入信号列表_${new Date().format('yyyyMMdd')}.csv`), content, 'UTF-8')
        }
    }

    async main(startDate = '2015-01-01', isCache = true) {
        this.isCache = isCache;
        await this.initPuppeteer({ headless: false })
        await this.getParams()
        await this.getHistory()
        await this.writeCsv(this.data)
        await this.browser.close();
    }
}

let controller = new Controller()
// controller.main();
module.exports = controller